using UnityEngine;
public class ShakeMgr : MonoBehaviour{
    public float shakeLevel = 3f;// 震动幅度
    public float setShakeTime = 0.5f;   // 震动时间
    public float shakeFps = 45f;    // 震动的FPS
    public bool isshakeCamera = false;// 震动标志
    private float fps;
    private float shakeTime = 0.0f;
    private float frameTime = 0.0f;
    private float shakeDelta = 0.005f;
    private Camera selfCamera;
    public static ShakeMgr ins;

    private void Awake() {
        ins = this;
    }

    public void OnShake(){
        isshakeCamera = true;
        selfCamera = gameObject.GetComponent<Camera>();
        shakeTime = setShakeTime;
        fps = shakeFps;
        frameTime = 0.03f;
        shakeDelta = 0.005f;
    }

    void Update(){
        if (isshakeCamera){
            if (shakeTime > 0){
                shakeTime -= Time.deltaTime;
                if (shakeTime <= 0){
                    isshakeCamera = false;
                }else{
                    frameTime += Time.deltaTime;
                    if (frameTime > 1.0 / fps){
                        frameTime = 0;
                        selfCamera.rect = new Rect(shakeDelta * (-1.0f + shakeLevel * Random.value), shakeDelta * (-1.0f + shakeLevel * Random.value), 1.0f, 1.0f);
                    }
                }
            }
        }
    }
}